package com.li.onjava8.concurrent.pl;

import java.util.Deque;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author li
 * @date 2023/10/30 10:38
 */
public class ParallelStreamPuzzle2 {
    public static final Deque<String> track = new ConcurrentLinkedDeque<>();

    static class IntGenerator implements Supplier<Integer> {
        private AtomicInteger current = new AtomicInteger();

        @Override
        public Integer get() {
            track.add(current.get() + ": " +Thread.currentThread().getName());
            return current.getAndIncrement();
        }
    }

    public static void main(String[] args) {
        List<Integer> list = Stream.generate(new IntGenerator())
                .limit(10)
                .parallel()
                .collect(Collectors.toList());
        System.out.println(list);
        track.forEach(System.out::println);
    }
}
